package com.zhouxl.finished;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author zhouxl
 * @DATE 2021/6/22
 * @descriptoin
 */
@Component
public class FinishedOrderService {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void saveOrder() {
        // 1.创建订单
        System.out.println("订单创建成功");
        // 2.发布事件
        ArrayList<String> contentList = new ArrayList<>();
        contentList.add("heling");
        contentList.add("123456789");
        FinishedOrderCreateEvent orderCreateEvent = new FinishedOrderCreateEvent(this, "订单创建", contentList);
        applicationEventPublisher.publishEvent(orderCreateEvent);
        // ApplicationContext是我们的事件容器上层，我们发布事件，也可以通过此容器完成发布
        // applicationEventPublisher.publishEvent(orderCreateEvent);//也可以
        System.out.println("finished!");
    }

}
